package com.hankol.winecellar.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hankol.winecellar.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 关忆北丶
 * @description:
 * @date :2020/7/23 16:14
 * 指示器View
 */
public class CommonIndicatorAdapter extends RecyclerView.Adapter<CommonIndicatorAdapter.IndicatorViewHolder> {
    private Activity mActivity;
    private LayoutInflater mInflater;
    private int position;
    private List<String> mUrls = new ArrayList<>();

    public CommonIndicatorAdapter(Activity mActivity, LayoutInflater mInflater) {
        this.mActivity = mActivity;
        this.mInflater = mInflater;
    }

    public void onRefreshAdapter(ArrayList<String> mUrls) {
        this.mUrls = mUrls;
        notifyDataSetChanged();
    }
    public void onRefreshAdapter(List<String> mUrls) {
        this.mUrls = mUrls;
        notifyDataSetChanged();
    }
    public void onRefreshAdapter(int position) {
        this.position = position;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public IndicatorViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new IndicatorViewHolder(mInflater.inflate(R.layout.item_indicator, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull IndicatorViewHolder indicatorViewHolder, @SuppressLint("RecyclerView") final int i) {
        Glide.with(mActivity.getApplicationContext())
                .load(mUrls.get(i))
                .placeholder(R.drawable.ic_cover_normal)
                .into(indicatorViewHolder.mIvIndicator);
        indicatorViewHolder.mIvIndicator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickLitener != null)
                    mOnItemClickLitener.onItemClick(i, mUrls.get(i));
            }
        });
        if (i == position){
            indicatorViewHolder.setCurrentViewAlpha(indicatorViewHolder.mIvIndicator);
        }else{
            indicatorViewHolder.setLastViewAlpha(indicatorViewHolder.mIvIndicator);

        }
    }

    @Override
    public int getItemCount() {
        return mUrls.size();
    }

    public class IndicatorViewHolder extends RecyclerView.ViewHolder {

        private ImageView mIvIndicator;

        public IndicatorViewHolder(@NonNull View itemView) {
            super(itemView);
            mIvIndicator = itemView.findViewById(R.id.iv_indicator);
        }
        private void setCurrentViewAlpha(ImageView view) {
            view.setAlpha(0.5f);
        }

        private void setLastViewAlpha(ImageView view) {
            view.setAlpha(1f);
        }


    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener litener) {
        mOnItemClickLitener = litener;
    }

    public interface OnItemClickLitener {
        void onItemClick(int position, String url);
    }
}
